//
//  ViewController.m
//  JSON解析
//
//  Created by hyl on 16/8/1.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"
#import "HYLVideo.h"
#import <UIImageView+WebCache.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MJExtension.h>
@interface ViewController ()<NSXMLParserDelegate>
/** videoArr*/
@property (nonatomic,strong) NSMutableArray *videoArr;
/** videoDic*/
//@property (nonatomic,strong) NSDictionary *videoDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self parseJSon2];
   }

-(NSMutableArray *)videoArr{
    if (_videoArr==nil) {
        _videoArr=[NSMutableArray array];
    }
    return _videoArr;
}

#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoArr.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"video"];
    HYLVideo *video=self.videoArr[indexPath.row];
    NSString *imageURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]];
//    cell.imageView.image=[UIImage imageNamed:video.image];
    
    cell.detailTextLabel.text=[NSString stringWithFormat:@"时长：%zd",video.length];
    cell.textLabel.text=video.name;
    return cell;
}



-(void) parseJSon2{
    //URＬ
    NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
    //新建一个请求对象
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSURLSession *session=[NSURLSession sharedSession];

    NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //
        NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data];
        //
        parser.delegate=self;
        //
        [parser parse];
//        [self.tableView reloadData];
    }];
    
    
    [dataTask resume];
}


#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HYLVideo *video=self.videoArr[indexPath.row];
    NSString *videoURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
    MPMoviePlayerViewController *vc=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:videoURL]];
    [self presentViewController:vc animated:YES completion:nil];

}
#pragma mark - NSXMLParserDelegate

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
//    NSLog(@"%@",elementName);
    if ([elementName isEqualToString:@"videos"])  return;
//    NSLog(@"%@",attributeDict);
    HYLVideo *video=[HYLVideo mj_objectWithKeyValues:attributeDict];
    [self.videoArr addObject:video];
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

}

-(void) parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidStartDocument");
}

-(void) parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidEndDocument");
    [self.tableView reloadData];
}
@end
